import { httpService } from "@/utils/httpService";

export function formatTime(date) {
    // console.log(333, date);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month < 10) {
        month = '0' + month
    }
    if (day < 10) {
        day = '0' + day
    }
    return year + '-' + month + '-' + day
}

export function formatTime1(value) {
    const date1 = new Date(value);

    const year1 = date1.getFullYear();
    const month1 = ('0' + (date1.getMonth() + 1)).slice(-2); // 月份从0开始，所以加1  
    const day1 = ('0' + date1.getDate()).slice(-2);
    const hours1 = ('0' + date1.getHours()).slice(-2); // 这已经是本地时间了，所以不需要进行时区转换  
    const minutes1 = ('0' + date1.getMinutes()).slice(-2);
    const seconds1 = ('0' + date1.getSeconds()).slice(-2);

    // 拼接成目标格式的字符串  
    return `${year1}-${month1}-${day1} ${hours1}:${minutes1}:${seconds1}`;
}

//解密
export async function decrypt(number) {
    let params = new FormData()
    params.append('encryptedData', number)
    let response = await httpService("/javaApi/crypto/decrypt", params, "post").then(
        (data) => {
            // console.log(String(data));
            return String(data)
        }
    );
    // console.log(123,"==",response);
    return response
}
// //解密
// export function decrypt(number) {
//     let params = new FormData()
//     params.append('encryptedData', number)
//     let response = httpService("/javaApi/crypto/decrypt", params, "post").then(
//         (data) => {
//             // console.log(String(data));
//             return String(data)
//         }
//     );
//     return response
// }

//加密
export function encrypt(number) {
    let params = new FormData()
    params.append('dataToEncrypt', number)
    let response = httpService("/javaApi/crypto/encrypt", params, "post").then(
        (data) => {
            // console.log(data)
            return data
        }
    );
    return response
}

//年级
export function getGradeIds() {
    let date = new Date();
    let nowYear = parseInt(date.getFullYear());
    let nowMonth = parseInt(date.getMonth() + 1);
    let gradeIds = [];
    if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
    } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
    }
    return gradeIds
}


//两校区人员 
export function editTeacherData(valueData) {
    // console.log("新数据", valueData);
    const convertedData = [];
    for (const schoolKey in valueData) {
        // console.log("学校", schoolKey, valueData);
        const school = valueData[schoolKey];
        const schoolObject = {
            label: school.name,
            value: schoolKey,
            children: [],
        };
        for (const departmentKey in school.departments) {
            const department = school.departments[departmentKey];
            const departmentObject = {
                label: departmentKey,
                value: departmentKey,
                departmentName: department.departmentName,
                children: [],
            };
            department.teachers.forEach(teacher => {
                const teacherObject = {
                    teacherName: teacher.teacherName,
                    teacherNumber: teacher.teacherNumber,
                    label: teacher.teacherName,
                    value: teacher.teacherNumber,
                };
                departmentObject.children.push(teacherObject);
            });
            schoolObject.children.push(departmentObject);
        }
        convertedData.push(schoolObject);
    }
    return convertedData
}

//file转base64
export function fileToBase64(file, callback) {
    var reader = new FileReader();
    reader.onload = function (e) {
        var base64String = e.target.result;
        // callback(base64String);
        return base64String
    };
    reader.readAsDataURL(file);
}

//file转blob
export function fileToblob(file, callback) {
    const reader = new FileReader();
    reader.onload = function (e) {
        const blob = e.target.result;
        return blob
    };
    reader.readAsArrayBuffer(file);
}

//base64转file
export function base64ToFile(base64Data, filename) {
    // 将base64的数据部分提取出来
    const parts = base64Data.split(';base64,');
    const contentType = parts[0].split(':')[1];
    const raw = window.atob(parts[1]);
    const rawLength = raw.length;
    const uInt8Array = new Uint8Array(rawLength);

    for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
    }

    // 使用Blob和File构造函数创建File对象
    const blob = new Blob([uInt8Array], { type: contentType });
    const file = new File([blob], filename, { type: contentType });

    return file;
}

//富文本传图片
export function uploadImg(value) {
    return new Promise((resolve, reject) => {
        var reader = new FileReader();
        reader.onload = function (e) {
            var base64String = e.target.result;
            resolve(base64String)
        };
        reader.readAsDataURL(value);
    });
}